﻿using Maticsoft.Model;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using UI.Forms;

namespace UI.Forms
{
    public partial class ProductAdd : Form
    {
        private bool _isEdit = false;
        private  Products _model = null;
        Maticsoft.BLL. Products  ProductDAL = new Maticsoft.BLL. Products();
        public  ProductAdd(bool isEdit = false,  Products model = null)
        {
            InitializeComponent();
            _isEdit = isEdit;
            _model = model;
            if (_isEdit && _model != null)
            {
                this.Text = "编辑用户";
                uiSymbolButton1.Text = "保存";
                uiTextBox1.Text = model. ProductId.ToString();
                uiTextBox2.Text = model. ProductName;
                uiTextBox3.Text = model. ProductType;
                uiTextBox8.Text = model.Description;
                uiTextBox7.Text = model.InterestRate;
                uiDatetimePicker1.Value = model.StartDate;
                uiDatetimePicker2.Value = model.EndDate;
                uiTextBox6.Text = model.MinimumInvestment.ToString();
                uiTextBox5.Text = model.MaximumInvestment.ToString();
                uiTextBox4.Text = model.CurrentValue.ToString();
                uiTextBox9.Text = model.InvestmentReturn.ToString();


            }
            else
            {
                this.Text = "添加用户";
                uiSymbolButton1.Text = "添加";
                uiDatetimePicker1.Value = DateTime.Now;
                uiTextBox1.Text = "";
                uiTextBox2.Text = "";
                uiTextBox3.Text = "";
                uiTextBox8.Text = "";
                uiTextBox7.Text ="";
                uiDatetimePicker1.Value = DateTime.Now;
                uiDatetimePicker2.Value = DateTime.Now;
                uiTextBox6.Text = "";
                uiTextBox5.Text = "";
                uiTextBox4.Text = "";
                uiTextBox9.Text = "";
            }

        }

        private void  ProductAdd_Load(object sender, EventArgs e)
        {

        }

        private void uiSymbolButton1_Click(object sender, EventArgs e)
        {
            

            if (_isEdit)  // 编辑
            {
                 Products model = new  Products()
                {
                    ProductId = _model. ProductId,
                    ProductName = uiTextBox2.Text,
                    ProductType = uiTextBox3.Text,
                    Description = uiTextBox8.Text,
                    InterestRate = uiTextBox7.Text,
                    StartDate = uiDatetimePicker1.Value,
                    EndDate = uiDatetimePicker2.Value,
                    MinimumInvestment = decimal.Parse(uiTextBox6.Text),
                    MaximumInvestment = decimal.Parse(uiTextBox5.Text),
                    CurrentValue = decimal.Parse(uiTextBox4.Text),
                    InvestmentReturn = decimal.Parse(uiTextBox9.Text)


                 };

                 ProductDAL.Update(model);
                DialogResult = DialogResult.OK;
            }
            else  // 添加
            {
                Maticsoft.Model. Products model = new  Products()
                {
                    ProductId = int.Parse(uiTextBox1.Text),
                    ProductName = uiTextBox2.Text,
                    ProductType = uiTextBox3.Text,
                    Description = uiTextBox8.Text,
                    InterestRate = uiTextBox7.Text,
                    StartDate = uiDatetimePicker1.Value,
                    EndDate = uiDatetimePicker2.Value,
                    MinimumInvestment = decimal.Parse(uiTextBox6.Text),
                    MaximumInvestment = decimal.Parse(uiTextBox5.Text),
                    CurrentValue = decimal.Parse(uiTextBox4.Text),
                    InvestmentReturn = decimal.Parse(uiTextBox9.Text)
                };
                 ProductDAL.Add(model);
                DialogResult = DialogResult.OK;

            }
        }
    }
}
